package com.augmentum.login.dao.impl.mybatis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.augmentum.login.Constants;
import com.augmentum.login.dao.BaseDao;
import com.augmentum.login.dao.UserDao;
import com.augmentum.login.model.Page;
import com.augmentum.login.model.User;
import com.augmentum.login.util.SqlMethod;
@Repository("userDao")
public class UserDaoImpl extends BaseDao implements UserDao {

    @Override
    public User getByName(String name) {
        return getSqlSession().selectOne(SqlMethod.USER_CLASS_NAME.getValue() + SqlMethod.GET_BY_NAME.getValue(), name);
    }

    @Override
    public void upatePassword(int id, String password) {
        Map<String , Object> map = new HashMap<String, Object>();
        map.put(Constants.ID, id);
        map.put(Constants.PASSWORD, password);
        getSqlSession().update(SqlMethod.USER_CLASS_NAME.getValue() + SqlMethod.UPDATE_PASSWORD.getValue(), map);
    }
    @Override
    public void upate(User user) {
        getSqlSession().update(SqlMethod.USER_CLASS_NAME.getValue() + SqlMethod.UPDATE.getValue(), user);
    }
    @Override
    public void save(User user) {
        getSqlSession().insert(SqlMethod.USER_CLASS_NAME.getValue() + SqlMethod.SAVE.getValue(), user);
    }

    @Override
    public List<User> findAll() {
        return getSqlSession().selectList(SqlMethod.USER_CLASS_NAME.getValue() + SqlMethod.FIND_ALL.getValue());
    }

    @Override
    public List<User> findBYpage(String name, String role, Page page) {
        if (page.getCurrentPage() > page.getPageCount()) {
            page.setCurrentPage(page.getPageCount());
        }
        Map<String , Object> map = new HashMap<String, Object>();
        map.put(Constants.USER_NAME, name);
        map.put(Constants.ROLE, role);
        map.put(Constants.OFFSET, page.getOffSet());
        map.put(Constants.PAGE_SIEZ, page.getPageSize());
        return this.getSqlSession().selectList(SqlMethod.USER_CLASS_NAME.getValue() + SqlMethod.FIND_BY_PAEG.getValue(), map);
    }

    @Override
    public void deleteBatch(String[] ids) {
        this.getSqlSession().delete(SqlMethod.USER_CLASS_NAME.getValue() + SqlMethod.DELETE_BATH.getValue(), ids);
    }
}
